package com.hui.dict;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hui.dict.bean.WordBean;
import com.hui.dict.db.DBManager;
import com.hui.dict.utils.URLUtils;

import java.util.ArrayList;
import java.util.List;

public class WordInfoActivity extends BaseActivity {
    TextView ziTv, pyTv, wbTv, bhTv, bsTV, syTv, jsTv;
    ListView jsLv;
    ImageView collectIv;
    String zi;
    List<String> mDatas;
    ArrayAdapter<String> adapter;

    List<String> jijie, xiangjie;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_word_info);
        // 接收上一个页面传递过来的对象
        Intent intent = getIntent();
        zi = intent.getStringExtra("zi");
        // 拼接网址
        String url = URLUtils.getWordurl(zi);

        initView();
        // 初始化listview显示的数据源
        mDatas = new ArrayList<>();
        adapter = new ArrayAdapter<>(this, R.layout.item_word_jslv, R.id.item_wordlv_tv, mDatas);
        jsLv.setAdapter(adapter);
        // 加载网络数据
        loadData(url);
    }

    @Override
    public void onSuccess(String json) {
        WordBean wordBean = new Gson().fromJson(json, WordBean.class);
        WordBean.ResultBean resultBean = wordBean.getResult();
        // 插入数据库
        DBManager.insertWordToWordtb(resultBean);
        // 将数据显示在view上
        notifyView(resultBean);
    }

    // 更新控件信息
    private void notifyView(WordBean.ResultBean resultBean) {
        ziTv.setText(resultBean.getZi());
        pyTv.setText(resultBean.getPinyin());
        wbTv.setText("五笔：" + resultBean.getWubi());
        bhTv.setText("笔画：" +resultBean.getBihua());
        bsTV.setText("部首：" +resultBean.getBushou());

        jijie = resultBean.getJijie();
        xiangjie = resultBean.getXiangjie();
        // 默认显示基本释义
        mDatas.clear();
        mDatas.addAll(jijie);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        // 联网失败，从数据库寻找
        WordBean.ResultBean bean = DBManager.queryWordFromWordtb(zi);
        if(bean != null) {
            notifyView(bean);
        }
    }

    public void initView() {
        ziTv = findViewById(R.id.word_tv_zi);
        pyTv = findViewById(R.id.word_tv_pinyin);
        wbTv = findViewById(R.id.word_tv_wubi);
        bhTv = findViewById(R.id.word_tv_bihua);
        bsTV = findViewById(R.id.word_tv_bushou);
        syTv = findViewById(R.id.word_tv_sy);
        jsTv = findViewById(R.id.word_tv_js);

        collectIv = findViewById(R.id.wordinfo_iv_collection);
        jsLv = findViewById(R.id.word_lv_js);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.wordinfo_iv_back:
                finish();
                break;
            case R.id.wordinfo_iv_collection:
                break;
            case R.id.word_tv_sy:
                syTv.setTextColor(Color.RED);
                jsTv.setTextColor(Color.BLACK);
                mDatas.clear();
                mDatas.addAll(jijie);
                adapter.notifyDataSetChanged();
                break;
            case R.id.word_tv_js:
                jsTv.setTextColor(Color.RED);
                syTv.setTextColor(Color.BLACK);
                mDatas.clear();
                mDatas.addAll(xiangjie);
                adapter.notifyDataSetChanged();
                break;
        }
    }
}